// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        _childList: [],
        _mainLayer: null,
        _windowShadow: null,
        _curWin: null,
        _windowMap: null,
        _hasKeyControl: null
    },

    registerWindow(name, type) {
        this._windowMap[name] = type;
    },

    init(main, shadow) {
        this._windowMap = {};
        this._hasKeyControl = null;
        this._mainLayer = main;
        this._windowShadow = shadow;
        this._mainLayer.on(cc.Node.EventType.TOUCH_START, function (event) {
            if (!this._mainLayer.active || !this.getCurWindow() || !this.getCurWindow()._itemNode.active) {
                return false;
            }
            var touches = event.getTouches();
            // var touchLoc = touches[0].getLocation();
            if (this.getCurWindow().getBuild() && this.getCurWindow().getBuild().getIsOutSizeEnable() && !this.getCurWindow()._itemNode.getBoundingBoxToWorld().contains(touches[0].getLocation())) {
                // this.getCurWindow().dismiss();
                this.dismissWindow(this.getCurWindow());
                return true;
            }
            if (this.getCurWindow().onTouchBegan)
                this.getCurWindow().onTouchBegan(event);
            return true;
        }.bind(this));


        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, function (event) {
            if (this._curWin) {
                event.stopPropagationImmediate();
                if (this._hasKeyControl) {
                    this._hasKeyControl.onKeyPressed(event.keyCode);
                } else if (this._curWin.onKeyPressed) {
                    this._curWin.onKeyPressed(event.keyCode);
                }
            }
        }, this);

    },

    getCurWindow() {
        return this._curWin;
    },

    initWindow(name, cb) {
        cc.loader.loadRes(name, cc.Prefab, (err, prefab) => {
            if (err != null) {
                if (cb) {
                    cb(err, null);
                }
                return;
            }
            cc.loader.setAutoRelease(prefab, true);
            var item = cc.instantiate(prefab);
            var js = item.getComponent(this._windowMap[name]);
            if (item && js) {
                js.setUINode(item);
                js.name = name;
                js._showOrder = 10;
                this.addWindow(name, js);
                this._curWin = js;
                if (cb) {
                    cb(null, js);
                }
            }
        });
    },

    addWindow(name, window) {
        this._childList.push(window);
        window._itemNode.active = false;
        if (this._mainLayer)
            this._mainLayer.addChild(window._itemNode)
    },

    getWindowByName(name) {
        if (this._childList) {
            for (var i = 0; i < this._childList.length; i++) {
                if (name === this._childList[i].name) {
                    return this._childList[i];
                }
            }
        }
        return null;
    },

    showWindowByNameWithDate(name, data) {
        var win = this.getWindowByName(name);
        if (win) {
            this.showWindow(win, false);
            win.show(data);
        } else {
            this.initWindow(name, function (err, js) {
                if (err != null)
                    return;
                this.showWindow(js, false);
                js.show(data);
            }.bind(this));
        }
    },

    showWindowByName(name, isAnim, isNext) {
        var win = this.getWindowByName(name);
        if (win) {
            this.showWindow(win, false);
        } else {
            this.initWindow(name, function (err, js) {
                if (err != null)
                    return;
                this.showWindow(js, false);
            }.bind(this));
        }
    },

    showWindow(window, isNext) {
        var maxOrder = 100;
        var isShow = true;
        if (this._curWin) {
            if (isNext) {
                maxOrder = this._curWin._showOrder - 1;
                for (var i = 0; i < this._children.length; i++) {
                    var item = this._children[i];
                    var showOrder = item._showOrder;
                    if (showOrder > 0 && showOrder < maxOrder) {
                        showOrder -= 1;
                        item._showOrder = showOrder;
                    }
                }
                isShow = false;
            } else {
                maxOrder = this._curWin._showOrder + 1;
                this._curWin._itemNode.active = false;
            }
        }
        window._showOrder = maxOrder;
        if (isShow) {
            this._curWin = window;
            this._curWin._itemNode.active = true;
            if (this._curWin.getBuild() && this._curWin.getBuild().getIsBackGroupGray()) {
                this._windowShadow.active = true;
            } else {
                this._windowShadow.active = false;
            }
            this._mainLayer.active = true;
        }
        this._hasKeyControl = null;
        if (this._curWin && this._curWin.getComponent('KeyControl')) {
            this._hasKeyControl = this._curWin.getComponent('KeyControl');
        }
    },

    isShow() {
        return this._mainLayer.active;
    },

    dismiss() {
        if (this._curWin) {
            this.dismissWindow(this._curWin);
        }
    },


    dismissWindowByName(name) {
        var win = this.getWindowByName(name);
        if (win) {
            this.dismissWindow(win);
        }
    },

    dismissWindow(win, id, data) {
        if (win) {
            if (this._mainLayer) {
                this._mainLayer.active = false;
            }
            if (win.getBuild() && win.getBuild().getIsDissRelease()) {
                for (var i = 0; i < this._childList.length; i++) {
                    if (this._childList[i] == win) {
                        this._childList.splice(i, 1);
                        break;
                    }
                }
                win._itemNode.destroy();
            } else {
                win._itemNode.active = false;
                win._showOrder = 0;
            }
            this._curWin = null;
            var maxOrder = 0;
            var showWin = null;
            for (var i = 0; i < this._childList.length; i++) {
                var item = this._childList[i];
                if (item._showOrder > maxOrder) {
                    maxOrder = item._showOrder;
                    showWin = item;
                }
            }
            this._hasKeyControl = null;
            if (showWin) {
                this._curWin = showWin;
                this._curWin._itemNode.active = true;
                if (this._curWin && this._curWin.getComponent('KeyControl')) {
                    this._hasKeyControl = this._curWin.getComponent('KeyControl');
                }
                if (this._curWin.getBuild() && this._curWin.getBuild().getIsBackGroupGray()) {
                    this._windowShadow.active = true;
                } else {
                    this._windowShadow.active = false;
                }
                this._mainLayer.active = true;
            } else {
                this._mainLayer.active = false;
            }
        }
    },


    showMessageBox(str, funOk, funCancel) {
        if (str == null)
            return;
        var msgWin = this.getWindowByName('msgwin');
        if (msgWin) {
            msgWin.showMsg(str, funOk, funCancel)
        } else {
            this.initWindow('msgwin', function (err, js) {
                if (err != null)
                    return;
                js.showMsg(str, funOk, funCancel)
            }.bind(this));
        }
    },

    showMessageBoxWithCancel(str, funOk) {
        var msgWin = WindowManager.getWindowById(GameConfig.WINDOW_MSG);
        if (msgWin) {
            msgWin.showWithCancelMsg(str, funOk);
        } else {
            cc.windowManager.initWindow('msgwin', function (err, js) {
                if (err != null)
                    return;
                js.showWithCancelMsg(str, funOk)
            }.bind(this));
        }
    },




    // WindowManager.dismissWindow(this, this.m_dismissId, this.m_dismissData);

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    // start () {

    // },

    update(dt) {
        // cc.log(" update!!");
    }
});